package com.kh.pajx.sop.view.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;

import com.kh.pajx.sop.base.ModelDrivenBaseAction;
import com.kh.pajx.sop.domain.HsEquOnline;
import com.kh.pajx.sop.domain.HsEquipmentInfo;
import com.kh.pajx.sop.domain.HsSchoolInfo;
import com.kh.pajx.sop.util.azzcsimp.DateUtils;
import com.opensymphony.xwork2.ActionContext;

/**
 * 在线监控状态Controller
 * 
 * @author 熊刘伟
 * @createDate 2014年7月4日 上午9:49:44
 * @return
 * @param
 */
@Controller
public class EquOnlieAction extends ModelDrivenBaseAction<HsEquOnline> {
	private String times;
	private List<String> timeList;
	private List<HsSchoolInfo> schoolList;
	private HttpServletRequest request;

	public String getSclEquNo() {
		try {
			request = ServletActionContext.getRequest();
			String sclId = request.getParameter("sclId");
			String sclName = request.getParameter("sclName");
			request.setAttribute("sclId", sclId);
			request.setAttribute("sclName", sclName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "list";
	}

	/**
	 * 获取学校列表
	 * 
	 */
	public String getSchool() {
		request = ServletActionContext.getRequest();
		String sclId = request.getParameter("sclId");
		try {
			timeList = onlineUI();
			if (getCurrentUser().isLookAll()) {
				schoolList = schoolService.findAllSchool();
			} else {
				schoolList = schoolService.getSclBySclIds(this.getCurrentUser().getSclIdList());
			}
			ActionContext.getContext().put("sclId", sclId);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return "list";

	}

	/**
	 * 通过设备学校编号、设备类型查询设备编号
	 */
	public String findEquNo() throws IOException {
		request = ServletActionContext.getRequest();
		String sclId = request.getParameter("sclId");
		String equType = (String) request.getParameter("equType");
		List<HsEquipmentInfo> list = hsEquipmentInfoService.findByHql(sclId, equType);
		ActionContext.getContext().put("list", list);
		return "equNos";
	}

	public String getSclid() {
		return "list";
	}

	// 用来生成状态集合
	public String findEquOnline() {
		// DwzAjaxResult result = new DwzAjaxResult();
		timeList = onlineUI();
		schoolList = schoolService.findAllSchool();
		request = ServletActionContext.getRequest();
		String sclId = request.getParameter("model.sclId");
		String equType = (String) request.getParameter("equType");
		String whatDay = (String) request.getParameter("whatDay");
		String equNo = request.getParameter("equNo") + "";
		// 获取验证信息
		String message = check(sclId, equType, equNo, whatDay);
		if (StringUtils.isNotEmpty(message)) {
			ActionContext.getContext().put("message", message);
		} else {
			List<HsEquOnline> equOnlines = hsEquOnlineService.findEquOnline(sclId, equType, equNo, whatDay);
			request.setAttribute("equOnlines", equOnlines);
			Map<String, Map<String, String>> staMap = null;
			if (StringUtils.isNotEmpty(whatDay) && !whatDay.equals("")) {
				staMap = handleResult(equOnlines);
			} else {
				staMap = handleResultNoDate(equOnlines);
			}
			request.setAttribute("staMap", staMap);
		}
		// 用于下拉框的回显
		// ActionContext.getContext().put("sclId", sclId);
		ActionContext.getContext().put("equType", equType);
		ActionContext.getContext().put("equNoOne", request.getParameter("equNo"));
		ActionContext.getContext().put("whatDay", whatDay);
		return "list_content";

	}

	/**
	 * 处理查询后的结果
	 * 
	 * @return
	 */
	public Map<String, Map<String, String>> handleResult(List<HsEquOnline> equOnlines) {
		// 用来存放日期和状态和时间点
		Map<String, Map<String, String>> map = new LinkedHashMap<String, Map<String, String>>();
		// 用來存放時間點和狀態
		// Map<String, String> statusMap = new HashMap<String, String>();
		for (int i = 0; i < equOnlines.size(); i++) {
			// 得到设备的插入表的完整时间("yyyyMMddHHmmss")
			String time = getMarryTime(equOnlines.get(i).getMonitorTime());
			// 得到设备的状态值
			String status = equOnlines.get(i).getOnLineStatusFlag();
			// 得到设备插入的日期("MMdd")
			String date = time.substring(4, 6) + "/" + time.substring(6, 8);
			// 得到设备插入的时间点("HHmm");
			String timeStamp = time.substring(8, 12);
			// statusMap.put(timeStamp, status);
			// 判断map集合里是否有该时间段的集合，若没有新增集合，并添加到map里，若有把现在的设备数据添加到状态map里去
			if (map.containsKey(date)) {
				Map<String, String> statusMap = map.get(date);
				statusMap.put(timeStamp, status);
			} else {
				Map<String, String> statusMap = new HashMap<String, String>();
				statusMap.put(timeStamp, status);
				map.put(date, statusMap);
			}
		}

		return map;
	}

	public String getMarryTime(String dateStr) {
		String newStr = dateStr;
		int lastMi = Integer.parseInt(dateStr.substring(11, 12));
		if (lastMi != 0 && lastMi != 5) {
			if (lastMi < 5) {
				newStr = (DateUtils.addMinute(dateStr, 5 - lastMi));
			} else {
				newStr = (DateUtils.addMinute(dateStr, 10 - lastMi));
			}
		}
		return newStr;
	}

	/**
	 * 处理查询后的结果当不传时间段的时候
	 */
	public Map<String, Map<String, String>> handleResultNoDate(List<HsEquOnline> equOnlines) {
		// 用来存放日期和状态和时间点
		Map<String, Map<String, String>> map = new LinkedHashMap<String, Map<String, String>>();
		// 用來存放時間點和狀態
		int size = equOnlines.size();
		for (int i = 0; i < size; i++) {
			// 得到设备的插入表的完整时间("yyyyMMddHHmmss")
			String time = getMarryTime(equOnlines.get(i).getMonitorTime());
			// 得到设备的状态值
			String status = equOnlines.get(i).getOnLineStatusFlag();
			// 得到设备插入的时间点("HHmm");
			String timeStamp = time.substring(8, 12);
			String eq = equOnlines.get(i).getEquNo() + "";
			// 判断map集合里是否有该设备的集合，若没有新增集合，并添加到map里，若有把现在的设备数据添加到状态map里去
			if (map.containsKey(eq)) {
				Map<String, String> statusMap = map.get(eq);
				statusMap.put(timeStamp, status);
				// map.put(eq, statusMap);
			} else {
				Map<String, String> statusMap = new HashMap<String, String>();
				statusMap.put(timeStamp, status);
				map.put(eq, statusMap);
			}
		}

		return map;
	}

	// 后台验证是否为空
	public String check(String sclId, String equType, String equNo, String whatDay) {
		if (StringUtils.isEmpty(sclId) || sclId.equals("")) {
			return "<span style='color:red;'>学校不能为空！</span>";
		} else {
			if (StringUtils.isEmpty(equType) || equType.equals("")) {
				return "<span style='color:red;'>设备类型不能为空</span>";
			} else {
				if (StringUtils.isNotEmpty(equNo) && !equNo.equals("") && !equNo.equals("0")
						&& (StringUtils.isEmpty(whatDay) || whatDay.equals(""))) {
					return "<span style='color:red;'>当设备编号为空的时候，时间段必须选择！</span>";
				}
				if ((StringUtils.isNotEmpty(whatDay) && !whatDay.equals(""))
						&& (equNo.equals("0") || equNo.equals("") || StringUtils.isEmpty(equNo))) {
					return "<span style='color:red;'>当时间段不为空的时候，设备编号必须选择！</span>";
				}
			}
		}

		return null;
	}

	public String getTimes() {
		return times;
	}

	public void setTimes(String times) {
		this.times = times;
	}

	public List<String> getTimeList() {
		return timeList;
	}

	public void setTimeList(List<String> timeList) {
		this.timeList = timeList;
	}

	public List<HsSchoolInfo> getSchoolList() {
		return schoolList;
	}

	public void setSchoolList(List<HsSchoolInfo> schoolList) {
		this.schoolList = schoolList;
	}

	/**
	 * 生成时间坐标
	 * */
	public List<String> onlineUI() {
		List<String> timeList = new LinkedList<String>();
		String h_str = "00";
		String m_str = "00";
		for (int i = 0; i < 24; i++) {
			if (i < 10) {
				h_str = "0" + i;
			} else {
				h_str = "" + i;
			}
			for (int j = 0; j < 60; j += 5) {
				if (j < 10) {
					m_str = "0" + j;
				} else {
					m_str = "" + j;
				}
				timeList.add(h_str + m_str);
			}
		}

		return timeList;
	}
}
